package com.example.demo1.controller.rest;

import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.entity.BaFunction;
import com.example.demo1.service.impl.service.FunctionService;
import com.example.demo1.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/function")
public class FunctionRestController {

    @Autowired
    private FunctionService functionService;

    @PostMapping("/addFunction")
    public ServerResponse addFunction(HttpServletRequest request, BaFunction function) {
        String userId;
        try {
            userId = JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")+"";
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "token已过期，请重新登录");
        }
        function.setCreateUser(userId);
        return functionService.addFunction(function);
    }

    @PostMapping("/validateFunctionName")
    public ServerResponse validateFunctionName(@RequestParam("functionName") String functionName) {
        return functionService.validateFunctionName(functionName);
    }

    @PostMapping("/deleteFunction")
    public ServerResponse deleteFunction(@RequestParam("functionId") Integer functionId) {
        return functionService.deleteFunction(functionId);
    }

    @PostMapping("/updateFunction")
    public ServerResponse updateFunction(HttpServletRequest request, BaFunction function) {
        String userId;
        try {
            userId = JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")+"";
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "token已过期，请重新登录");
        }
        function.setCreateUser(userId);
        return functionService.updateFunction(function);
    }

    @PostMapping("/validateFunctionNameExSelf")
    public ServerResponse validateFunctionNameExSelf(@RequestParam("functionName") String functionName,
                                                     @RequestParam("functionId") Integer functionId) {
        return functionService.validateFunctionNameExSelf(functionName, functionId);
    }

    @PostMapping("/getAllFunctions")
    public ServerResponse<List<BaFunction>> getAllFunctions() {
        return functionService.getAllFunctions();
    }

    @PostMapping("/getAllFunctionsByParamLimit")
    public ServerResponse getAllFunctionsByParamLimit(@RequestParam(value = "searchStr", required=false) String searchStr,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize) {
        return functionService.getAllFunctionsByParamLimit(searchStr, pageNum, pageSize);
    }

}
